package com.xiangban.yanglaodajie_platform.controller;

import com.xiangban.yanglaodajie_platform.aop.Permission;
import com.xiangban.yanglaodajie_platform.entity.Orders;
import com.xiangban.yanglaodajie_platform.entity.Refund;
import com.xiangban.yanglaodajie_platform.entity.dto.OrderNoDto;
import com.xiangban.yanglaodajie_platform.entity.dto.RefundDto;
import com.xiangban.yanglaodajie_platform.entity.dto.ShoppingCartDto;
import com.xiangban.yanglaodajie_platform.entity.res.ApiResult;
import com.xiangban.yanglaodajie_platform.entity.res.ApiResultCode;
import com.xiangban.yanglaodajie_platform.service.OrdersService;
import com.xiangban.yanglaodajie_platform.utils.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Map;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/3/24 9:41
 */
@RequestMapping("/api/orders")
@RestController
@Api(tags = "订单管理")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @PostMapping("/addOrderList")
    @ApiOperation("添加至订单")
    @Permission
    public JsonData addOrderList(@RequestBody ShoppingCartDto shoppingCartDto) throws ParseException {
        Object save = ordersService.save(shoppingCartDto);
        if (save.equals(-1)){
            return JsonData.buildError("已结算");
        } else if (save.equals(-2)){
            return JsonData.buildError("商品店铺类型不统一");
        }else if (save.equals(-3)){
            return JsonData.buildError("收货地址id是空");
        }else if (save.equals(-4)){
            return JsonData.buildError("包含的商品中已消失");
        }else if (save.equals(-5)){
            return JsonData.buildError("包含的商品中库存不足");
        }else if (save.equals(-6)){
            return JsonData.buildError("商家已下线");
        }
        return save == null ? JsonData.buildError("库存不足") : JsonData.buildSuccess(save,"订单编号");
    }


    @PostMapping("/getOrdersList")
    @ApiOperation("获取所有订单")
    @Permission
    public JsonData getOrdersList(@RequestBody Orders orders){
       return JsonData.buildSuccess(ordersService.getList(orders),"操作成功");
    }

    @PostMapping("/getOrdersInfo")
    @ApiOperation("获取订单详情")
    @Permission
    public ApiResult<Object> getOrdersInfo(@RequestBody Orders orders){

        return ApiResult.newSuccess(ordersService.getOrdersInfo(orders));
    }

    @PostMapping("/getShopOrdersInfo")
    @ApiOperation("商家获取订单详情")
    @Permission(type = "shop")
    public ApiResult<Object> getShopOrdersInfo(@RequestBody Orders orders){

        return ApiResult.newSuccess(ordersService.getShopOrdersInfo(orders));
    }

    @PostMapping("/updateIsAfhalen")
    @ApiOperation("修改订单状态")
    @Permission
    public ApiResult<Object> updateIsAfhalen(@RequestBody OrderNoDto orderNoDto){
        ordersService.updateIsAfhalen(orderNoDto);
        return ApiResult.newSuccess();
    }

    @PostMapping("/deleteOrder")
    @ApiOperation("删除订单")
    @Permission()
    public ApiResult<Object> deleteOrder(@RequestBody Orders orders){
        ordersService.deleteOrder(orders);
        return ApiResult.newSuccess();
    }

    @PostMapping("/updateStatus")
    @ApiOperation("修改订单状态(商家端)")
    @Permission(type = "shop")
    public ApiResult<Object> updateStatus(@RequestBody Orders orders){
        ordersService.updateStatus(orders);
        return ApiResult.newSuccess();
    }


    @PostMapping("/shopIncome")
    @ApiOperation("店铺收入(商家端)")
    @Permission(type = "shop")
    public ApiResult<Object> shopIncome(@RequestBody Orders orders){
        return ApiResult.newSuccess(ordersService.todayIntegral(orders));
    }

//    @PostMapping("/todayOrdersNumber")
//    @ApiOperation("今日订单量(商家端)")
//    @Permission(type = "shop")
//    public ApiResult<Object> todayOrdersNumber(@RequestBody Orders orders){
//        return ApiResult.newSuccess(ordersService.todayOrdersNumber(orders));
//    }
//
//    @PostMapping("/ordersListIntegral")
//    @ApiOperation("总收入(商家端)")
//    @Permission(type = "shop")
//    public ApiResult<Object> allIntegral(@RequestBody Orders orders){
//        return ApiResult.newSuccess(ordersService.allIntegral(orders));
//    }

    @PostMapping("/ordersRefund")
    @ApiOperation("点击退款(用户操作)")
    @Permission()
    public ApiResult<Object> ordersRefund(@RequestBody RefundDto refund){
        ordersService.Refund(refund);
        return ApiResult.newSuccess();
    }

    @PostMapping("/refundInfo")
    @ApiOperation("退款详情")
    @Permission()
    public ApiResult<Object> refundInfo(@RequestBody RefundDto refund){

        return ApiResult.newSuccess(ordersService.refundInfo(refund));
    }

    @PostMapping("/isRefund")
    @ApiOperation("退款操作（商家端）")
    @Permission(type = "shop")
    public ApiResult<Object> isRefund(@RequestBody RefundDto refundDto){
        ordersService.isRefund(refundDto);
        return ApiResult.newSuccess();
    }

    @PostMapping("/orderInfoByOrderNumber")
    @ApiOperation("扫码获取订单（商家端）")
    @Permission(type = "shop")
    public ApiResult<Object> orderInfoByOrderNumber(@RequestBody Map<String,Object> map){
        return ApiResult.newSuccess(ordersService.orderInfoByOrderNumber(map));
    }

    @PostMapping("/destroyOrder")
    @ApiOperation("核销订单（商家端）")
    public ApiResult<Object> destroyOrder(@RequestBody OrderNoDto orderNoDto){
        ordersService.destroyOrder(orderNoDto);
        return ApiResult.newSuccess();
    }
}
